package com.ruoyi.RDTeam.service.impl;

import com.ruoyi.RDTeam.domain.MemberInfo;
import com.ruoyi.RDTeam.mapper.MemberInfoMapper;
import com.ruoyi.RDTeam.service.IMemberInfoService;
import com.ruoyi.common.utils.AvatarUpLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * 研发团队成员展示Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class MemberInfoServiceImpl implements IMemberInfoService
{
    @Autowired
    private MemberInfoMapper memberInfoMapper;


    @Autowired
    private AvatarUpLoadUtil avatarUploadUtil;  // 注入 AvatarUploadUtil 实例
    /**
     * 查询研发团队成员展示
     *
     * @param memberId 研发团队成员展示主键
     * @return 研发团队成员展示
     */
    @Override
    public MemberInfo selectMemberInfoByMemberId(String memberId)
    {
        return memberInfoMapper.selectMemberInfoByMemberId(memberId);
    }

    /**
     * 面向 姓名、专业、擅长字段的模糊查询
     * @param name
     * @param major
     * @param expertise
     * @return
     */
    @Override
    public List<MemberInfo> searchList(String name, String major, String expertise) {
        return memberInfoMapper.searchList(name, major, expertise);
    }
    /**
     * 查询研发团队成员展示列表
     *
     * @param memberInfo 研发团队成员展示
     * @return 研发团队成员展示
     */
    @Override
    public List<MemberInfo> selectMemberInfoList(MemberInfo memberInfo)
    {
        return memberInfoMapper.selectMemberInfoList(memberInfo);
    }

    /**
     * 新增研发团队成员展示
     *
     * @param memberInfo 研发团队成员展示
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo)
    {
        if (memberInfo.getAvatarFile() != null && !memberInfo.getAvatarFile().isEmpty()) {
            // 上传头像文件并获取文件 URL

           try (InputStream inputStream = memberInfo.getAvatarFile().getInputStream()) {
                // 读取图片的字节数据
                byte[] fileBytes = inputStream.readAllBytes();

                // 调用上传方法
               String pictureUrl = avatarUploadUtil.uploadFileToOSS(fileBytes, memberInfo.getPhone(),memberInfo.getPictureUrl());
               // 将文件 URL 存储到数据库中
               memberInfo.setPictureUrl(pictureUrl);
               System.out.println("头像上传成功");

            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("图片上传失败: " + e.getMessage(), e);
            }

        }

        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改研发团队成员展示
     *
     * @param memberInfo 研发团队成员展示
     * @return 结果
     */
    @Override
    public int updateMemberInfo(MemberInfo memberInfo)
    {
        if (memberInfo.getAvatarFile() != null && !memberInfo.getAvatarFile().isEmpty()) {
            // 上传头像文件并获取文件 URL

            try (InputStream inputStream = memberInfo.getAvatarFile().getInputStream()) {
                // 读取图片的字节数据
                byte[] fileBytes = inputStream.readAllBytes();

                // 调用上传方法
                String pictureUrl = avatarUploadUtil.uploadFileToOSS(fileBytes, memberInfo.getPhone(),memberInfo.getPictureUrl());
                // 将文件 URL 存储到数据库中
                memberInfo.setPictureUrl(pictureUrl);
                System.out.println("头像上传成功");

            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("图片上传失败: " + e.getMessage(), e);
            }

        }else {
            // 未修改头像时，保留原有 pictureUrl
            memberInfo.setPictureUrl(memberInfo.getPictureUrl());
        }
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }

    /**
     * 批量删除研发团队成员展示
     *
     * @param memberIds 需要删除的研发团队成员展示主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberIds(String[] memberIds)
    {
        return memberInfoMapper.deleteMemberInfoByMemberIds(memberIds);
    }

    /**
     * 删除研发团队成员展示信息
     *
     * @param memberId 研发团队成员展示主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberId(String memberId)
    {
        return memberInfoMapper.deleteMemberInfoByMemberId(memberId);
    }
}
